<?php
/**
 * Environment.php - 27 Jan 2009
 * 
 * @author Kevin Breed
 */

class KB_Environment
{
	/**
	 * @var string	The servername of the localhost
	 */
	public $localhostServerName = 'localhost';
	
	/**************/
	/* EXCEPTIONS */
	/**************/
	
	/**
	 * @var int Exception code: Property cannot be accessed, due to the unexistence
	 * 							of the property itself, or set or get method
	 */
	const E_UNACCESSABLE_PROPERTY = 1;
	
	
	/*************************/
	/* OVERLOADING MECHANISM */
	/*************************/
	
	/**
	 * Magic get function.
	 */
	public function __get ( $name )
	{		
		// return class property if it exists
		if ( property_exists( $this, $name ) ) return $this->$name;
		
		// otherwise get property by get function
		$getFunc	= $this->formatGetFunctionName( $name );
		$className 	= get_class( $this );
			
		if ( !method_exists( $this, $getFunc ) )
		{
			throw new Exception ( 	"The property '$name' is not a valid " .
									"property in class '$className'",
									self::E_UNACCESSABLE_PROPERTY );
			return;
		}
			
		return call_user_func( array( $this, $getFunc ) );
	}
	
	/**
	 * Formats the name of the get function, based on the given varname.
	 * 
	 * @param	string	the name of the var
	 * @return 	string 	the name of the get function
	 */
	public function formatGetFunctionName ( $varName )
	{
		return 'get' . ucfirst( $varName );
	}
	
	/**
	 * Formats the name of the set function, based on the given varname.
	 * 
	 * @param	string	the name of the var
	 * @return 	string 	the name of the set function
	 */
	public function formatSetFunctionName ( $varName )
	{
		return 'set' . ucfirst( $varName );
	}
	
	/**
	 * Magic set function.
	 */
	public function __set ( $name, $value )
	{
		if ( property_exists( $this, $name ) ) 
		{
			$this->$name = $value;
			return;
		}
		
		$setFunc	= $this->formatSetFunctionName( $name );
		$className 	= get_class( $this );
		
		if ( !method_exists( $this, $setFunc ) )
		{
			throw new Exception ( 	"The property '$name' cannot be set in " .
									"class '$className'.",
									self::E_UNACCESSABLE_PROPERTY );			
			return;
		}
		
		call_user_func( array( $this, $setFunc ), $value );
	}
	
	/**
	 * Magic isset function.
	 */
	public function __isset ( $name )
	{
		return method_exists( $this, $this->formatGetFunctionName( $name ) );
	}
	
	/**
	 * Magic unset function.
	 */
	public function __unset ( $name )
	{
		call_user_func( array( $this, $this->formatSetFunctionName( $name ) ), '' );
	}
	
	
	/***********************/
	/* GETTERS AND SETTERS */
	/***********************/
	
	/**
	 * Returns the $_SERVER[ 'DOCUMENT_ROOT' ] with or without a trailing slash
	 * 
	 * @param 	bool	$trailingSlash	Indication whether to return the doc
	 * 									root with or without a trailing slash
	 * @return	string					The document root path with or without
	 * 									trailing slash
	 */
	public function getDocRoot ( $trailingSlash = true )
	{
		if ( $trailingSlash )
			 return ( substr( $_SERVER[ 'DOCUMENT_ROOT' ], -1 ) != '/' ) ? 
							$_SERVER[ 'DOCUMENT_ROOT' ] . '/' :
							$_SERVER[ 'DOCUMENT_ROOT' ];
							
		else
			return ( substr( $_SERVER[ 'DOCUMENT_ROOT' ], -1 ) == '/' ) ? 
						substr( $_SERVER[ 'DOCUMENT_ROOT' ], 0, -1 ) :
						$_SERVER[ 'DOCUMENT_ROOT' ]; 
	}
	
	
	/**********/
	/* CHECKS */
	/**********/
	
	/**
	 * Check whether this thing is running on a local server or somewhere else.
	 * 
	 * @return bool true when running local, false otherwise.
	 */
	public function isLocal ()
	{
		return ( $_SERVER[ 'SERVER_NAME' ] == $this->localhostServerName );
	}
}

?>